﻿

#region Using directives

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


#endregion

namespace FXnaEngine
{
   public class FSkyCubeSE:FFileBasedSE
    {


       protected FSkyCubeSE():base()

       {




           cube = FEngineState.Content.Load<Model>("Models/Cube");

           skyCubeMapTexture = FEngineState.Content.Load<TextureCube>("Images/SkyCubeMap");

           effect = FEngineState.Content.Load<Effect>("Effects/PreScreenSkyCubeMapping");


 
       }

       static FSkyCubeSE defaultSkyCubeSE;

       public static FSkyCubeSE DefaultSkyCubeSE
       {
           get {

               if (defaultSkyCubeSE==null)
               {
                   defaultSkyCubeSE = new FSkyCubeSE();
                   
               }
               
               return FSkyCubeSE.defaultSkyCubeSE; }
         
       }



       /// <summary>
       /// Shader effect filename.
       /// </summary>
       const string Filename = "PreScreenSkyCubeMapping.fx";

       /// <summary>
       /// Sky cube map texture filename.
       /// </summary>
       const string SkyCubeMapFilename = "SkyCubeMap";

       /// <summary>
       /// Default sky color
       /// </summary>
       static readonly Color DefaultSkyColor = new Color(255, 255, 255);

       /// <summary>
       /// The Cube Map texture for the sky!
       /// </summary>
       private TextureCube skyCubeMapTexture = null;

       /// <summary>
       /// Sky cube map texture
       /// </summary>
       /// <returns>Texture cube</returns>
       public TextureCube SkyCubeMapTexture
       {
           get
           {
               return skyCubeMapTexture;
           }
       }

       private Model cube;




       /// <summary>
       /// Render sky with help of shader.
       /// </summary>
       public void RenderSky(Color setSkyColor)
       {
           // Can't render with shader if shader is not valid!
           if (this.Valid == false)
               return;



           //渲染前需要关闭深度缓存，渲染后需要打开

           FEngineState.Device.DepthStencilState = DepthStencilState.None;

         









           // Set effect parameters
            // AmbientColor = setSkyColor;

           effect.Parameters["diffuseTexture"].SetValue(skyCubeMapTexture);

           effect.Parameters["projection"].SetValue(FEngineState.ProjectionMatrix);
           effect.Parameters["view"].SetValue(FEngineState.ViewMatrix);
           effect.Parameters["ambientColor"].SetValue(DefaultSkyColor.ToVector4());

        
           

           // Override model's effect and render
           cube.Meshes[0].MeshParts[0].Effect = effect;
           cube.Meshes[0].Draw();




           FEngineState.Device.DepthStencilState = DepthStencilState.Default;

         

          

       }

       /// <summary>
       /// Render sky
       /// </summary>
       public void RenderSky()
       {
           RenderSky(DefaultSkyColor);
       }



    }
}
